﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;

namespace QuanLyBanHang.GUI
{
    public partial class frmDoTuoi : Form
    {
        private int flag;
        DataTable dataTable;
        public frmDoTuoi()
        {
            InitializeComponent();
            disableControler();
            loadDataTable();
        }
        private void disableControler()
        {
            txtMadt.Enabled = false;
            txtTendt.Enabled = false;
            txtGhichu.Enabled = false;
            btnLuu.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnThemMoi.Enabled = true;
        }

        private void enableControler()
        {
            txtMadt.Enabled = true;
            txtGhichu.Enabled = true;
            txtTendt.Enabled = true;
            btnThemMoi.Enabled = false;


        }
        private void textChange()
        {
            txtGhichu.Text = "";
            txtMadt.Text = "";
            txtTendt.Text = "";

        }
        private void loadDataTable()
        {
            dataTable = KetNoi.GetTable("select * from tblDoTuoi");
            dtgDoTuoi.DataSource = dataTable;
            dtgDoTuoi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataTable = null;
            dtgDoTuoi.AllowUserToAddRows = false;
            dtgDoTuoi.AllowUserToDeleteRows = false;
            dtgDoTuoi.Columns["MaDoTuoi"].HeaderText = "Mã độ tuổi";
            dtgDoTuoi.Columns["MaDoTuoi"].Width = 100;
            dtgDoTuoi.Columns["TenDoTuoi"].HeaderText = "Tên độ tuổi";
            dtgDoTuoi.Columns["TenDoTuoi"].Width = 350;
            dtgDoTuoi.Columns["GhiChu"].HeaderText = "Ghi chú";
            dtgDoTuoi.Columns["GhiChu"].Width = 350;
        }       

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int kq;
            kq = KetNoi.ExcuteSQL("delete from tblKhoSP where MaKho = '" + txtMadt.Text + "'");
            if (kq > 0)
            {

                MessageBox.Show("Thông tin kho đã được xóa");
                loadDataTable();
                textChange();
                disableControler();
            }
            else
            {
                MessageBox.Show("Lỗi sảy ra.");
            }

        }
        private void txtloc_TextChanged(object sender, EventArgs e)
        {
            if (txtloc.Text != null)
            {
                dataTable = KetNoi.GetTable("select * from tblDoTuoi where MaDoTuoi like '%" + txtloc.Text + "%' or TenDoTuoi like'%" + txtloc.Text + "%'");
                dtgDoTuoi.DataSource = dataTable;
            }
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            flag = 1;
            enableControler();
            textChange();
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            int kq =0;
            if (flag == 1)
            {
                kq = KetNoi.ExcuteSQL("insert into tblDoTuoi values('" + txtMadt.Text + "',N'" + txtTendt.Text + "',N'" + txtGhichu.Text + "')");
                if (kq > 0)
                {

                    if (MessageBox.Show("Thông tin đã được lưu. Bạn có muốn tiếp tục thêm ?", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        disableControler();
                    }

                    loadDataTable();
                    textChange();
                    kq = 0;
                }
                else
                {
                    MessageBox.Show("Lỗi sảy ra.");
                }
            }
            else 
            {
                kq = KetNoi.ExcuteSQL("update tblDoTuoi set TenDoTuoi=N'" + txtTendt.Text + "',GhiChu=N'" + txtGhichu.Text + "' where MaDoTuoi = '" + txtMadt.Text + "'");
                if (kq > 0)
                {

                    MessageBox.Show("Thông tin đã được sửa");
                    loadDataTable();
                    textChange();
                    disableControler();
                    kq = 0;
                }
                else
                {
                    MessageBox.Show("Lỗi sảy ra.");
                }
            }        
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flag = 2;
            enableControler();
            txtMadt.Enabled = false;
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            
        }

        private void btnXoa_Click_1(object sender, EventArgs e)
        {
            int kq;
            kq = KetNoi.ExcuteSQL("delete from tblDoTuoi where MaDoTuoi = '" + txtMadt.Text + "'");
            if (kq > 0)
            {

                MessageBox.Show("Thông tin kho đã được xóa");
                loadDataTable();
                textChange();
                disableControler();
            }
            else
            {
                MessageBox.Show("Lỗi sảy ra.");
            }
        }

        private void dtgDoTuoi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow gridrow = dtgDoTuoi.CurrentRow;
                txtMadt.Text = gridrow.Cells["MaDoTuoi"].Value.ToString();
                txtTendt.Text = gridrow.Cells["TenDoTuoi"].Value.ToString();
                txtGhichu.Text = gridrow.Cells["GhiChu"].Value.ToString();
                txtMadt.Enabled = false;
                btnLuu.Enabled = false;
                btnThemMoi.Enabled = true;
                btnXoa.Enabled = true;
                btnSua.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
